<template>
	<div class="content-flag">
		<lang></lang>
		<curr-app></curr-app>
		<list-title :data="header" @changeVisible="changeVisible"></list-title>
		<list-table :list="contentFlagList" :tableLoading="tableLoading" :appid="$route.query.appid">
			<template v-slot:data>
				<el-table-column prop="flagname" label="属性名称" show-overflow-tooltip :render-header="icons">
					<template slot-scope="scope">
						<el-input v-model="scope.row.flagname" @blur="setFlagname(scope.$index, scope.row)" placeholder="请输入属性名称"></el-input>
					</template>
				</el-table-column>
				<!-- <el-table-column prop="flag" label="属性值"></el-table-column> -->
				<el-table-column label="操作" width="200">
					<template slot-scope="scope">
						<el-popover
						placement="top"
						width="160"
						v-model="scope.row.delTipStatus" v-if="auth.delete">
							<p class="del-tip"><span class="el-icon-question"></span>确定删除吗？</p>
							<div style="text-align: right; margin: 0">
								<el-button size="mini" type="text" @click="scope.row.delTipStatus = false">取消</el-button>
								<el-button type="primary" size="mini" @click="handleDelete(scope.$index, scope.row)">确定</el-button>
							</div>
							<el-button type="text" slot="reference" :disabled='scope.row.delBtnStatus'>
								删除 <span class="el-icon-loading" v-if='scope.row.delBtnStatus'></span>
							</el-button>
						</el-popover>
						<el-button type='text' size="mini" @click="getFlagContentList(scope.row)">内容列表</el-button>
						<el-button size='mini' type='text' class='tag-read' @click='getTag(scope.row)'>标签调用</el-button>
					</template>
				</el-table-column>
			</template>
		</list-table>
		<!-- 添加变量 开始 -->
		<el-dialog :title="dialogTitle" :visible.sync="header.dialogs[0].visible" :close-on-click-modal="false" width="460px">
			<el-form :model="contentFlagForm" :rules="rules" ref="contentFlagForm" label-width="100px">
				<el-form-item label="属性名称" prop="data.flagname" style="width: 378px;">
					<el-input v-model="contentFlagForm.data.flagname" placeholder="属性名称"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click="header.dialogs[0].visible = false">取 消</el-button>
				<el-button type="primary" @click="submitForm('contentFlagForm')" :disabled='contentFlagForm.submitLock'>
					确 定 <span class="el-icon-loading" v-if='contentFlagForm.submitLock'></span>
				</el-button>
			</div>
		</el-dialog>
		<!-- 添加变量 结束 -->
	</div>
</template>

<script>
import ListTitle from "pc/common/Title"
import ListTable from "pc/common/ListTable"
import {judgeAuth} from "static/js/common.js"
import {ERROR} from "static/js/constant.js"
import {mapGetters} from 'vuex'
import Clipboard from 'clipboard'
import Lang from "pc/common/Lang"
import CurrApp from "pc/common/CurrApp"

export default {
	inject: ['reload'],
	data() {
		return {
			/*appid*/
			appid: this.$route.query.appid,
			/*头部数据*/
			header: {
				title: '内容属性',
				reload: true,
				dialogs: [
					{
						title: '添加属性',
						type: 'primary',
						visible: false,
						auth: false,
					}
				]
			},
			auth: {
				editor: false,//编辑
				delete: false,//删除
			},
			dialogTitle: '添加属性',//dialog标题
			/*列表数据*/
			contentFlagList: [
				// {
				// 	id: 1,
				// 	flag: '',
				// 	flagname: '',
				// 	sys: 1,
				// 	delBtnStatus: false,//删除按钮状态
				// 	delTipStatus: false,//删除提示状态
				// },
			],
			tableLoading: true,
			/*添加表单数据*/
			contentFlagForm: {
				submitLock: false,
				data: {
					id: 0,
					flagname: '',
				}
			},
			rules: {
				data: {
					flagname: [
						{ required: true, message: '请输入属性名称', trigger: 'blur' },
						{ min: 1, max: 16, message: '长度在 1 到 16 个字符', trigger: 'blur' },
					]
				}
			}
		};
	},
	components: {
		ListTitle,
		ListTable,
		Lang,
		CurrApp
	},
	created: function() {
		/*获取列表数据*/
		this.getContentFlagList()
		/*权限判断显示*/
		this.header.dialogs[0].auth = (judgeAuth('addContentFlag', this.allMenuList) == undefined)?false:true
		this.auth.editor = (judgeAuth('modifyContentFlag', this.allMenuList) == undefined)?false:true
		this.auth.delete = (judgeAuth('deleteContentFlag', this.allMenuList) == undefined)?false:true
	},
	watch: {
		
	},
	methods: {
		icons(h, {column}){
			const inReview = '直接在输入框中修改属性名称，鼠标离开即可保存'
			return h(
				'div', [
					h('span', column.label),
					h('el-tooltip', {
						props: {
							placement: 'right'
						}
					}, [
						h('div', {
							slot: 'content',
							style: {
								'width': '150px',
								'whiteSpace': 'normal',
								'margin-bottom': '10px',
								'line-height': '20px',
								'height': '30px',
							}
						}, inReview),
						h('i', {
							class: 'el-icon-info',
							style: 'margin-left:5px; font-size: 12px'
						})
					],)
				]
			)
		},
		/*取出列表数据*/
		getContentFlagList: function(){
			if(this.$api.contentFlagList != undefined){
				this.$baseURL(this.$domain.cms)
				this.$get(this.$api.contentFlagList, this.$route.query).then(res => {
					// console.log(res);
					if(res.data.code === 0){
						if(res.data.data != undefined){
							this.contentFlagList = []
							Object.keys(res.data.data).forEach((k) => {
								this.contentFlagList.push ({
									id: res.data.data[k].id,
									flag: res.data.data[k].flag,
									flagname: res.data.data[k].flagname,
									sys: res.data.data[k].sys,
									delBtnStatus: false,//删除按钮状态
									delTipStatus: false,//删除提示状态
								});
							})
							this.pagination = res.data.data.pageInfo
						}
					}
				})
				.catch(err => {
					let error = ERROR.SER404
					if(err.response != undefined  && err.response.status == 500){
						error = ERROR.SER500
					}
					this.$message({
						showClose: true,
						message: error,
						type: 'error'
					});
				})
				.finally(() => {
					this.tableLoading = false
				}) 
			}
		},
		/*dialog切换 方便子组件调用*/
		changeVisible: function(index){
			/*因为子组件调用的都是添加的行为，所以需要把app_cat_id置为0(方便判断是添加还是修改)，title改为'添加分类'*/
			this.dialogTitle = '添加变量'
			this.contentFlagForm.data.id = 0

			this.header.dialogs[index].visible = !this.header.dialogs[index].visible
		},
		/*提交添加、修改表单*/
		submitForm(formName) {
			if(this.contentFlagList.length >= 5){
				this.$message({
					showClose: true,
					message: '最多设置5个',
					type: 'error'
				});
				return false;
			}
			if(!this.contentFlagForm.submitLock){
				this.$refs[formName].validate((valid) => {
					if (valid) {
						this.contentFlagForm.submitLock = true;
						/*添加、修改分类*/
						let apiUrl = this.$api.addContentFlag//默认是添加
						if(this.contentFlagForm.data.id > 0){//修改
							apiUrl = this.$api.modifyContentFlag
						}

						if(apiUrl != undefined){
							this.$baseURL(this.$domain.cms)
							this.$post(apiUrl, {
								appid: this.appid,
								ContentFlag: this.contentFlagForm.data
							}).then(res => {
								// console.log(res);
								/*成功*/
								if(res.data.code == 0){
									this.getContentFlagList()
									this.$message({
										showClose: true,
										message: res.data.info,
										type: 'success'
									});

									if(this.contentFlagForm.data.id > 0){
										this.reload();
									}else{
										this.$refs[formName].resetFields();
									}
								}else{
									this.$message({
										showClose: true,
										message: res.data.info,
										type: 'error'
									});
								}
							})
							.catch(err => {
								let error = ERROR.SER404
								if(err.response != undefined  && err.response.status == 500){
									error = ERROR.SER500
								}
								this.$message({
									showClose: true,
									message: error,
									type: 'error'
								});
							})
							.finally(() => {
								this.contentFlagForm.submitLock = false;
							}) 
						}
					} else {
						console.log('error submit!!');
						return false;
					}
				});
			}
		},
		/*修改属性名称*/
		setFlagname(index, row) {
			if(this.$api.modifyContentFlag != undefined){
				this.$baseURL(this.$domain.cms)
				this.$post(this.$api.modifyContentFlag, {
					appid: this.appid,
					'ContentFlag': {
						'id': row.id,
						'flagname': row.flagname
					}
				}).then(res => {
					if(res.data.code == 0){
						this.$message({
							showClose: true,
							message: res.data.info,
							type: 'success'
						});
					}else{
						this.$message({
							showClose: true,
							message: res.data.info,
							type: 'error'
						});
					}
				})
				.catch(err => {
					let error = ERROR.SER404
					if(err.response != undefined  && err.response.status == 500){
						error = ERROR.SER500
					}
					this.$message({
						showClose: true,
						message: error,
						type: 'error'
					});
				})
				.finally(() => {}) 
			}
		},
		/*删除*/
		handleDelete(index, row) {
			if(this.$api.deleteContentFlag != undefined){
				if(!this.contentFlagList[index].delBtnStatus){
					this.contentFlagList[index].delBtnStatus = true//锁住当前的 删除按钮
					this.contentFlagList[index].delTipStatus = false//提示消失
					/*请求后台删除*/
					this.$baseURL(this.$domain.cms)
					this.$get(this.$api.deleteContentFlag, {
						appid: this.appid,
						'id': row.id
					}).then(res => {
						if(res.data.code == 0){
							this.$message({
								showClose: true,
								message: res.data.info,
								type: 'success'
							});
							this.contentFlagList.splice(index, 1);//删除当前行
						}else{
							this.$message({
								showClose: true,
								message: res.data.info,
								type: 'error'
							});
						}
					})
					.catch(err => {
						let error = ERROR.SER404
						if(err.response != undefined  && err.response.status == 500){
							error = ERROR.SER500
						}
						this.$message({
							showClose: true,
							message: error,
							type: 'error'
						});
					})
					.finally(() => {
						this.contentFlagList[index].delBtnStatus = false//释放当前的 删除按钮
					}) 
				}
			}
		},
		/*调用标签*/
		getTag(row){
			let self = this;
			var content = row.flag;
			var clipboard = new Clipboard('.tag-read', {
				text: function() {
					return "{us:attrcontent flag='" + content + "'}";
				}
			});

			clipboard.on('success', e => {
				self.$message({
					message: '复制标签成功!',
					type: 'success'
				});
				//tips(this, '复制链接成功!', 'success');
				clipboard.destroy()   // 释放内存
			})
			clipboard.on('error', e => {
				// 不支持复制
				self.$message({
					message: '该浏览器不支持链接复制!',
					type: 'warning'
				});
				//tips(this, '该浏览器不支持链接复制!', 'warning');
				clipboard.destroy()    // 释放内存
			})
		},
		getFlagContentList(row){
			// console.log(row.id)
			let query = {
				'appid': this.$route.query.appid,
				'id': row.id
			}
			this.$router.push({
				name: 'pc/cms/instance/config/FlagContentList',
				query: query
			})
		}
	},
	computed: {
		...mapGetters([
			'allMenuList'
		])
	}
};
</script>

<style lang="stylus" scoped>
/*操作按钮 平等距离*/
.cell
	>.el-button + .el-button
		margin-left: 0;
</style>